package swd.work.generator.v2.java.bean;

import java.io.*;
import java.util.*;

import com.pub.database.*;

import swd.work.generator.v2.java.base.*;

public class ViewBean extends BeanExtend implements BeanImplement {

    public ViewBean(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(pqbnDAO, pstrUserNO);
    }

    /**
     * outputDefaultConstructor
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputConstructor(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public View(){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * setMutltiLang
     *
     * @param lbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void outputMutltiLang(BufferedWriter pbwrPrgNO, String pstrPrgNO,
                                  String pstrSysNO) throws Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1 = null;
        Hashtable<String,Object> lhtWKF_FRM1_DATA = null;
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM0 = null;
        Hashtable<String,Object> lhtWKF_FRM0_DATA = null;
        Hashtable<String,Hashtable<String,Object>> lhtWKF_PRG_BUTTON = null;

        String lstrBtnName = null;
        int lintLoop_cnt = 0;

        try {
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "protected void setMultiLang(){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "ResourceBundle rbdTitleResource=ResourceBundle.getBundle(\"com\\\\swd\\\\" +
                                 pstrSysNO + "\\\\" + pstrPrgNO +
                                 "\\\\Multi-lang\",Locale.getDefault());", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{",
                                 2);

            //Hidden不用輸出多國語言
            lhtWKF_FRM1 = gqbnDAO.hashtableSQL(
                    "select fld_no from wkf_frm1 where prg_no='" + pstrPrgNO +
                    "' and fld_no is not null and rw_mode<>'H' group by fld_no");

            while (lhtWKF_FRM1 != null &&
                   lhtWKF_FRM1.get(String.valueOf(lintLoop_cnt)) != null) {

                lhtWKF_FRM1_DATA = (Hashtable<String,Object>) lhtWKF_FRM1.get(String.valueOf(
                        lintLoop_cnt));

                lintLoop_cnt++;

                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                     "set" +
                                     lhtWKF_FRM1_DATA.get("FLD_NO").toString().substring(0,1).toUpperCase() +
                                     lhtWKF_FRM1_DATA.get("FLD_NO").toString().substring(1) +
                                     "_title(rbdTitleResource.getString(\"" +
                                     lhtWKF_FRM1_DATA.get("FLD_NO").toString() +
                                     "_title\"));", 1);
            }

            lhtWKF_FRM0 = gqbnDAO.hashtableSQL(
                    "select frm_no from wkf_frm0 where prg_no='" + pstrPrgNO +
                    "'");

            lintLoop_cnt = 0;

            while (lhtWKF_FRM0 != null &&
                   lhtWKF_FRM0.get(String.valueOf(lintLoop_cnt)) != null) {
                lhtWKF_FRM0_DATA = (Hashtable<String,Object>) lhtWKF_FRM0.get(String.valueOf(
                        lintLoop_cnt));

                lintLoop_cnt++;

                super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                     "setFrm_" +
                                     lhtWKF_FRM0_DATA.get("FRM_NO").toString() +
                                     "_name(rbdTitleResource.getString(\"frm_" +
                                     lhtWKF_FRM0_DATA.get("FRM_NO").toString() +
                                     "_name\"));", 1);
            }

            lhtWKF_PRG_BUTTON=gqbnDAO.hashtableSQL("select special_name_e from wkf_prg_button where prg_no='" + pstrPrgNO + "' and status='" + super.gstrPrgKind + "'");

            if(lhtWKF_PRG_BUTTON!=null && lhtWKF_PRG_BUTTON.size()>0){
                for(int i=0;i<lhtWKF_PRG_BUTTON.size();i++){
                    lstrBtnName = ((Hashtable<String,Object>)lhtWKF_PRG_BUTTON.get(String.valueOf(i))).get("SPECIAL_NAME_E").toString();

                    super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                         "setBtn_" + lstrBtnName + "_title(rbdTitleResource.getString(\"Btn_" + lstrBtnName + "_Title\"));",
                                         1);
                }
            }

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "setPrgTitle(rbdTitleResource.getString(\"prgTitle\"));",
                                 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "catch(Exception e){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "e.printStackTrace();", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        } catch (Exception e) {
            throw new Exception(e.toString());
        } finally {
            if (lhtWKF_FRM1_DATA != null) {
                lhtWKF_FRM1_DATA.clear();
            }
            if (lhtWKF_FRM1 != null) {
                lhtWKF_FRM1.clear();
            }
            if (lhtWKF_FRM0_DATA != null) {
                lhtWKF_FRM0_DATA.clear();
            }
            if (lhtWKF_FRM0 != null) {
                lhtWKF_FRM0.clear();
            }
            if(lhtWKF_PRG_BUTTON!=null){
                lhtWKF_PRG_BUTTON.clear();
            }

            lhtWKF_FRM1_DATA = null;
            lhtWKF_FRM1 = null;
            lhtWKF_FRM0_DATA = null;
            lhtWKF_FRM0 = null;
            lhtWKF_PRG_BUTTON = null;
        }
    }

    /**
     * setPageFieldsVar
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void setPageFieldsVar(BufferedWriter pbwrPrgNO, String pstrPrgNO) throws
            Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1 = null;
        Hashtable<String,Object> lhtWKF_FRM1_DATA = null;
        int lintWKF_FRM1_cnt = 0;
        String lstrFldType = null;

        try {
            lhtWKF_FRM1 = gqbnDAO.hashtableSQL(
                    "select distinct wkf_frm1.fld_no,wkf_frm1.fld_kind,wkf_frm1.fld_len from wkf_frm1 where wkf_frm1.prg_no='" +
                    pstrPrgNO +
                    "' and wkf_frm1.fld_no is not null");

            while (lhtWKF_FRM1 != null &&
                   lhtWKF_FRM1.get(String.valueOf(lintWKF_FRM1_cnt)) != null) {

                lhtWKF_FRM1_DATA = (Hashtable<String,Object>) lhtWKF_FRM1.get(String.valueOf(
                        lintWKF_FRM1_cnt));

                lintWKF_FRM1_cnt++;

                lstrFldType = getFldType(lhtWKF_FRM1_DATA.get("FLD_KIND").toString(),lhtWKF_FRM1_DATA.get("FLD_LEN").toString());

                outputGetterAndSetterCode(pbwrPrgNO, lstrFldType,
                                          lhtWKF_FRM1_DATA.get("FLD_NO").
                                          toString());
                outputGetterAndSetterCode(pbwrPrgNO, "String",
                                          lhtWKF_FRM1_DATA.get("FLD_NO").
                                          toString() + "_title");
                outputGetterAndSetterCode(pbwrPrgNO, "boolean",
                                          lhtWKF_FRM1_DATA.get("FLD_NO").
                                          toString() + "_readonly");
            }
        } catch (Exception e) {
            throw new Exception(e.toString());
        } finally {
            if (lhtWKF_FRM1_DATA != null) {
                lhtWKF_FRM1_DATA.clear();
            }
            if (lhtWKF_FRM1 != null) {
                lhtWKF_FRM1.clear();
            }

            lhtWKF_FRM1_DATA = null;
            lhtWKF_FRM1 = null;
        }
    }

    public String createBean(String pstrPrgNO) {
        String lstrCreateResult = "";
        String lstrBeanFileName = "View.java";
        BufferedWriter lbwrPrgNO = null;

        try {
            super.setBaseVarible(pstrPrgNO);

            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "JSP_FRAME_PATH",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\",
                                          gstrUserNO);

            super.outputBeanHeader(lbwrPrgNO, super.getSysNO(gstrUserNO),
                                   pstrPrgNO, lstrBeanFileName, gstbTabStr,
                                   gstrTabChar);



            //輸出Default Constructor
            outputConstructor(lbwrPrgNO);

            //輸出AlertMsg,PrgAction,PrgStatus,FlowAction,FlowStatus,FlowNO 等getter and setter
            setCommVarible(lbwrPrgNO);

            setPageFormTitle(lbwrPrgNO,pstrPrgNO);

            //output getter,setter and readonly getter,setter mapping for page fields.
            setPageFieldsVar(lbwrPrgNO, pstrPrgNO);

            //output config button
            outputPageButton(lbwrPrgNO,pstrPrgNO);

            //output MultiLang method
            outputMutltiLang(lbwrPrgNO, pstrPrgNO, super.getSysNO(gstrUserNO));

            //output setDBProperties method
            outputDBProperties(lbwrPrgNO);

            //output setMessage method
            outputMessage(lbwrPrgNO,super.getSysNO(gstrUserNO),pstrPrgNO);

            //output set readonly method
            //outputSetFldReadOnly(lbwrPrgNO);

            //output class end single
            super.outputJavaCode(lbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception e) {
                System.out.println("cant flush View.java File!!");
                e.printStackTrace();
            }
        }
        
        return lstrCreateResult;
    }

    /**
     * outputDBProperties
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputDBProperties(BufferedWriter pbwrPrgNO) throws Exception {
        try {
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "protected void setDBProperties(){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "ResourceBundle rbdTitleResource=ResourceBundle.getBundle(\"WEB_Config\",Locale.getDefault());",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{",
                                 2);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "setDb_alias(rbdTitleResource.getString(\"DB_ALIAS\"));",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "setDb_debug(Boolean.valueOf(rbdTitleResource.getString(\"DEBUG\")).booleanValue());",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "setSource_charset(rbdTitleResource.getString(\"SOURCE_CHARSET\"));",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "setTarget_charset(rbdTitleResource.getString(\"TARGET_CHARSET\"));",
                                 1);

            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "catch(Exception e){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "e.printStackTrace();", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "finally{", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "rbdTitleResource=null;", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    /**
     * outputMessage
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputMessage(BufferedWriter pbwrPrgNO,String pstrSysNO,String pstrPrgNO) throws Exception {
        try {
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "public String getMessage(String pstrKey){", 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return ResourceBundle.getBundle(\"com\\\\swd\\\\"  + pstrSysNO + "\\\\" + pstrPrgNO + "\\\\Multi-lang\",Locale.getDefault()).getString(pstrKey);",
                                 1);
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    /**
     * outputPageButton
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void outputPageButton(BufferedWriter pbwrPrgNO, String pstrPrgNO) throws Exception{
        Hashtable<String,Hashtable<String,Object>> lhtWKF_PRG_BUTTON=null;
        String lstrBtnName=null;
        try{
            lhtWKF_PRG_BUTTON=gqbnDAO.hashtableSQL("select special_name_e from wkf_prg_button where prg_no='" + pstrPrgNO + "' and status='" + super.gstrPrgKind + "'");

            if(lhtWKF_PRG_BUTTON==null || lhtWKF_PRG_BUTTON.size()==0){
                return;//沒有設定button
            }

            for(int i=0;i<lhtWKF_PRG_BUTTON.size();i++){
                lstrBtnName = ((Hashtable<String,Object>)lhtWKF_PRG_BUTTON.get(String.valueOf(i))).get("SPECIAL_NAME_E").toString();

                outputGetterAndSetterCode(pbwrPrgNO,"String","btn_" + lstrBtnName + "_title");
                outputGetterAndSetterCode(pbwrPrgNO,"String","btn_" + lstrBtnName + "_show");
            }

        }
        catch(Exception e){
            throw new Exception(e);
        }
        finally{
            if(lhtWKF_PRG_BUTTON!=null){
                lhtWKF_PRG_BUTTON.clear();
                lhtWKF_PRG_BUTTON = null;
            }
        }
    }

    /**
     * setPageFormTitle
     *
     * @param lbwrPrgNO BufferedWriter
     * @param pstrPrgNO String
     */
    private void setPageFormTitle(BufferedWriter lbwrPrgNO, String pstrPrgNO) throws Exception {
        Hashtable<String,Hashtable<String,Object>> lhtWKF_FRM1=null;

        try{
            lhtWKF_FRM1=gqbnDAO.hashtableSQL("select frm_no from wkf_frm0 where prg_no='" + pstrPrgNO + "'");

            if(lhtWKF_FRM1==null || lhtWKF_FRM1.get("0")==null){return;}

            for(int i=0;i<lhtWKF_FRM1.size();i++){
                outputGetterAndSetterCode(lbwrPrgNO,"String","frm_" + ((Hashtable<String,Object>)lhtWKF_FRM1.get(String.valueOf(i))).get("FRM_NO") + "_name");
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw new Exception(e);
        }
        finally{
            if(lhtWKF_FRM1!=null){
                lhtWKF_FRM1.clear();
                lhtWKF_FRM1=null;
            }
        }
    }

    /**
     * outputGetterAndSetterCode
     *
     * @param pbwrPrgNO BufferedWriter 輸出物件
     * @param pstrVarName String 要輸出的getter and setter method name
     */
    private void outputGetterAndSetterCode(BufferedWriter pbwrPrgNO,
                                          String pstrVarType,
                                          String pstrVarName) throws Exception {
        //String lstrSimpBeginnerVarType = super.getSimpBeginnerVarType(
        //        pstrVarType);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "private " + pstrVarType + " " + pstrVarName + ";", 1);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public " + pstrVarType + " get" + pstrVarName.substring(0,1).toUpperCase() + pstrVarName.substring(1) +
                             "(){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return this." + pstrVarName + ";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void set" + pstrVarName.substring(0,1).toUpperCase() + pstrVarName.substring(1) + "(" +
                             pstrVarType + " " + pstrVarName + "){", 1);
        if (pstrVarType.equals("String")) {
            super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                                 "if(" + pstrVarName +
                                 "!=null && " +
                                 pstrVarName +
                                 ".equals(\"null\")){" +
                                 pstrVarName + "=null;}",
                                 1);
        }
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "this." + pstrVarName +
                             "=" + pstrVarName + ";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputGetterAndSetterCode
     *
     * @param pbwrPrgNO BufferedWriter 輸出物件
     * @param pstrVarName String 要輸出的getter and setter method name

    private void outputGetterAndSetterReadOnlyCode(BufferedWriter pbwrPrgNO,
                                                  String pstrVarType,
                                                  String pstrVarName) throws
            Exception {
        String lstrSimpBeginnerVarType = super.getSimpBeginnerVarType(
                pstrVarType);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "private " + pstrVarType + " g" +
                             lstrSimpBeginnerVarType + pstrVarName +
                             "_readonly" + ";", 1);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public " + pstrVarType + " get" + pstrVarName +
                             "_readonly" +
                             "(){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return this.g" + lstrSimpBeginnerVarType +
                             pstrVarName + "_readonly;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void set" + pstrVarName + "_readonly" +
                             "(" +
                             pstrVarType + " p" + lstrSimpBeginnerVarType +
                             "Parameter){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
        //                       "if((p" + lstrSimpBeginnerVarType +
        // "Parameter!=null && p" + lstrSimpBeginnerVarType +
        //        "Parameter.toUpperCase().equals(\"READONLY\")) || (p" +
        // lstrSimpBeginnerVarType + "Parameter==null || p" +
        //                       lstrSimpBeginnerVarType +
        //                       "Parameter.trim().equals(\"\"))){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "this.g" + lstrSimpBeginnerVarType + pstrVarName +
                             "_readonly=p" + lstrSimpBeginnerVarType +
                             "Parameter;", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "else{", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                               "throw new Exception(\"set" + pstrVarName +
                               "_readOnly value is not avalible!!\");", 1);
        //super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }
*/
    /**
     * outputCommGetterAndSetter
     */
    private void setCommVarible(BufferedWriter pbwrPrgNO) throws Exception {

        outputGetterAndSetterCode(pbwrPrgNO, "String", "onloadScript");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "prgAction");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "prgStatus");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "flowAction");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "prgTitle");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "data_kind");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "loginUserNO");

        if(super.gblnFlowYN){
            outputGetterAndSetterCode(pbwrPrgNO, "String", "flowStatus");
            outputGetterAndSetterCode(pbwrPrgNO, "String", "flowNO");
            outputGetterAndSetterCode(pbwrPrgNO, "String", "seqno");
            outputGetterAndSetterCode(pbwrPrgNO, "String", "portal");
        }

        outputGetterAndSetterCode(pbwrPrgNO, "QueryBean", "dAO");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "db_alias");
        outputGetterAndSetterCode(pbwrPrgNO, "boolean", "db_debug");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "source_charset");
        outputGetterAndSetterCode(pbwrPrgNO, "String", "target_charset");
    }


}
